<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class DictionaryApiController extends AbstractRestfulController
{
    public function getList()
    {	
		$dictionaryTable=$this->getServiceLocator()->get('Models\Model\DictionaryFactory');
		$getDictionaryword = $dictionaryTable->getDictionaryword()->toarray();	
		if(count($getDictionaryword)!=0){
			return new JsonModel(array(
				'dictionaryList'	=> json_encode($getDictionaryword),
				'value' 			=> 1,
			));			
		}else{
			return new JsonModel(array(
				'value'		 => 0,
				'data'  	 => 'No Data',
			));			
		}
    }
    public function get($searchword)
    {
		header('Access-Control-Allow-Origin: *');		
		$dictionaryTable=$this->getServiceLocator()->get('Models\Model\DictionaryFactory');	
		$getDictionaryData = $dictionaryTable->getdictionaryData($searchword)->toarray();	
		if(count($getDictionaryData)!=0){
			$dictionaryword=str_word_count($searchword);
			if($dictionaryword==1){
				$word=$searchword;
			}else{
				$explodeword=explode(' ',$searchword);
				$word = $explodeword[0].'+'.$explodeword[1];
			}
			if(isset($word)!=""){
				$url = "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=".$word;
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $url);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				$body = curl_exec($ch);
				curl_close($ch);
				$json = json_decode($body, true);
				$correctImageresults = $json['responseData']['results'][0]['tbUrl'];
				if($correctImageresults==""){
					//$correct_width = $json['responseData']['results'][0]['width'];
					//$correct_height = $json['responseData']['results'][0]['height'];
					$image= "<img src=".$correctImageresults." >";
				}else{
					$results = $json['responseData']['results'][0]['tbUrl'];
					//$width = $json['responseData']['results'][0]['tbWidth'];
					//$height = $json['responseData']['results'][0]['tbHeight'];
					$image= "<img src=".$results.">";
				}
			}
			return new JsonModel(array(
				'worddata' 	=> json_encode($getDictionaryData),
				'image'		=> $image,
				'value' 	=> 1,
			));			
		}else{
			return new JsonModel(array(
				'value' 	=> 0,
				'data'  	=> 'No Data',
			));			
		}
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');
		$dictionaryword=str_word_count($data['word']);
		if($dictionaryword==1){
			$word=$data['word'];
		}else{
			$explodeword=explode(' ',$data['word']);
			$word = $explodeword[0].'+'.$explodeword[1];
		}
		if(isset($word)!=""){
			$url = "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=".$word;
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$body = curl_exec($ch);
			curl_close($ch);
			$json = json_decode($body, true);
			$correctImageresults = $json['responseData']['results'][0]['tbUrl'];
			if($correctImageresults==""){
				$correct_width = $json['responseData']['results'][0]['width'];
				$correct_height = $json['responseData']['results'][0]['height'];
				$image= "<img src=".$correctImageresults." width=".$width." height=".$height." >";
				if($image){
					return new JsonModel(array(
						'image' 		=> $image ,
						'value' 		=> 1,
					));			
				}
			}else{
				$width = $json['responseData']['results'][0]['tbWidth'];
				$height = $json['responseData']['results'][0]['tbHeight'];
				$image= "<img src=".$correctImageresults." width=".$width." height=".$height." >";
				if($image){
					return new JsonModel(array(
						'image' 		=> $image ,
						'value' 		=> 1,
					));			
				}
			}
		}
	}
    public function update($id, $data)
    {
        $dictionaryTable=$this->getServiceLocator()->get('Models\Model\DictionaryFactory'); 
		$getDictionarylist = $dictionaryTable->getdictionarylist()->toarray();	
		if(count($getDictionarylist)!=0){
			return new JsonModel(array(
				'dictionaryList'	=> json_encode($getDictionarylist),
				'value' 			=> 1,
			));			
		}else{
			return new JsonModel(array(
				'value'		 => 0,
				'data'  	 => 'No Data',
			));			
		}
    }
    public function delete($id)
    {
       
    }
}